<?php


namespace app;
use app\Model\MemberModel;
use app\Model\ParkMemberModel;
use app\Model\ParkOrderModel;
use vender\Config;

/**
 * Class Payment
 * Author daichen
 * @package app
 * 付款-》主动缴费即不写成API
 * 混合主动支付 用户出场-》模板消息-》生成订单-》订单页面（JSSDK微信支付+积分抵扣）
 */
class ParkPayment {


    /**
     * @author:橙橙同学
     * @func:检查停车场顾客
     */
    protected function checkParkCustomer($plateNumber,$amount,$memberInfo){
        // 判断用户是否绑定车牌及开启使用积分抵扣
        if (!$memberInfo) {
            echo "未绑定微信";
            exit();
        }
    }


    /**
     * @author:橙橙同学
     * @func:生成停车订单
     */
    public static function generateParkOrder($uid,$amount=0)
    {

        //检测会员合法性
        $memberInfo = MemberModel::getMemberInfoByPlateNumber($plateNumber);

        self::checkParkCustomer($plateNumber,$amount,$memberInfo);

        //查询会员停车专用积分
        $uid = $memberInfo['uid'];
        $parkMemberInfo = ParkMemberModel::getParkMemberInfoByUid($uid);
        // 1小时 == 300积分 == 3.00 元 停车会员消费积分
        $useScore = $amount*100;

        //插入订单信息
        $insertOrderRes = ParkOrderModel::insertParkOrder($memberInfo,$parkMemberInfo,$useScore,$amount);

        if($insertOrderRes)
        {
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $plateNumber
     * @param $amount
     * @return mixed|string
     * @author:橙橙同学
     * @func:积分缴费
     */
    public static function scorePay($park_order_id,$uid,$plateNumber,$amount,$useScore){
        $parkMemberInfo = ParkMemberModel::getParkMemberInfoByUid($uid);
        // 用户停车专用积分

        $memberScore = array();
        if(Config::get('PARKSCOREPAY')){
            $parkScore = empty($parkMemberInfo['score']) ? 0 : $parkMemberInfo['score'];
            $memberScore['parkscore'] = $parkScore;
        }

        //如果开启ERP积分抵扣
        if(Config::get('ERPSCOREPAY')){
            //获取会员erp积分
            $erpScore = getErpScore();
            //会员所有积分
            $memberScore['erpscore'] = $erpScore;
        }

        $allScore = 0;
        foreach ($memberScore as $key=>$value)
        {
            $allScore+=$value;
        }

        //如果用户的各项积分不为空并且各项积分和大于所需积分
        if($allScore >=$useScore) {
            //如果停车积分不为空 并且大于0
            if (!empty($memberScore['parkscore']) || $memberScore['parkscore'] > 0){
                //扣积分 优先扣除停车积分 再扣除ERP积分
                //如果停车积分不为空
                if($memberScore['parkscore']>=$useScore){
                    //如果停车积分大于所需积分 直接扣除停车积分


                }else{
                    //扣干净停车积分 继续扣除ERP积分

                }
            }else{
                //如果停车积分为0 或停车积分抵扣被系统关闭 纯ERP抵扣 此时$allScore即为erpScore

            }

        }else{
        //如果用户的各项积分加起来小于所需积分则启用 积分+钱


        }
    }



    /**
     * 设置积分
     * @param string $cardid    会员卡号
     * @param string $scoreNum  积分数量
     * @return mixed
     */
    public static function setCustomerScore($cardid = '0',$scoreNum = '0')
    {
        $cardApi = new CardApi();
        return $cardApi->setCustomerScore($cardid,$scoreNum);
    }

    /**
     * 积分同步
     * @param $uid
     * @param $openid
     *
     * @return bool|mixed
     */
    public static function syncScore($uid, $openid)
    {
        $db = self::db();
        $card = self::getCardInfo($uid);
        if (!empty($card)) {
            $cardApi          = new CardApi();
            $getScoreByCustId = $cardApi->getCustomerScore($card['custid']);

            if ($getScoreByCustId > 0) {
                $checkUser = $db->count('ims_mc_members_score', array('uid' => $uid));
                if ($checkUser == 0) {
                    $data = array(
                        'uid'         => $uid,
                        'openid'      => $openid,
                        'custid'      => $card['custid'],
                        'cardsn'      => $card['cardsn'],
                        'score'       => $getScoreByCustId,
                        'create_time' => time(),
                        'sync_time'   => time()
                    );

                    $sync_id = $db->insert('ims_mc_members_score', $data);
                    return  $sync_id ? $getScoreByCustId : false;

                } else {
                    $result = $db->update('ims_mc_members_score', array('score' => $getScoreByCustId,'sync_time'=>time()), array('uid' => $uid));
                    return ($result !== false) ? $getScoreByCustId : false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    public static function getCardInfo($uid)
    {
        $db       = self::db();
        $cardInfo = $db->get('ims_mc_card_members', array('custid', 'cardsn'), array('uid' => $uid));

        if (!empty($cardInfo)) {
            return $cardInfo;
        } else {
            return false;
        }
    }

    private static function db()
    {
        return new medoo(C('mysql'));
    }


}